global class ES_BidReceivedNotification implements Messaging.InboundEmailHandler {
	global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.InboundEnvelope envelope) {
    	Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();
      	try {
        	 RawNotification__c r = new RawNotification__c();
         	r.XML__c = email.plainTextBody;
         	insert r;
         	result.success = true;
      	} catch (Exception e) {
        	System.debug('MailHandler failed to process message: ' + e);
      	}
       	return result;
   	}
    
   	private GetItemResponseType.GetItemResponseType_e xmlToItem(String xml) {
   		GetItemResponseType.GetItemResponseType_e resp = new GetItemResponseType.GetItemResponseType_e();
   	 	XMLDom d = new XMLDom(xml);	
   	 	resp.setTimeStamp(Datetime.valueOf(d.getElementByTagName('TimeStamp').nodeValue));
   	 	resp.setNotificationEventName(d.getElementByTagName('NotificationEventName').nodeValue);
   	 	XMLDom.Element xmlItem = d.getElementByTagname('Item');

   	 	ItemType.ItemType_e item = new ItemType.ItemType_e();
   	 	resp.setItem(item);
   	 	item.setItemID(xmlItem.getValue('ItemID'));

   	 	ListingDetailsType.ListingDetailsType_e ld = new ListingDetailsType.ListingDetailsType_e();
   	 	item.setListingDetails(ld);
   	 	ld.setStartTime(Datetime.valueOf(xmlItem.getValue('StartTime')));
   	 	ld.setEndTime(Datetime.valueOf(xmlItem.getValue('EndTime')));
   	 	ld.setViewItemURL(xmlItem.getValue('ViewItemURL'));
   	 	ld.setHasUnansweredQuestions(Boolean.valueOf(xmlItem.getValue('HasUnansweredQuestions')));
   	 	
   	 	SellingStatusType.SellingStatusType_e status = new SellingStatusType.SellingStatusType_e();
   	 	item.setSellingStatus(status);
   	 	XMLDom.Element xStatus = xmlItem.getElementByTagName('SellingStatus');
   	 	status.setBidIncrement(xStatus.getValue('BidIncrement'));
   	 	status.setBidCount(Integer.valueOf(xStatus.getValue('BidCount')));
   	 	status.setCurrentPrice(xStatus.getValue('CurrentPrice'));
   	 	
   	 	UserType.UserType_e bidder = new UserType.UserType_e();
   	 	status.setHighBidder(bidder);
   	 	XMLDom.Element xBidder = xmlItem.getElementByTagName('HighBidder');
   	 	bidder.setEIASToken(xBidder.getValue('EIASToken'));
   	 	bidder.setEmail(xBidder.getValue('Email'));
		bidder.setFeedbackScore(Integer.valueOf(xBidder.getValue('FeedbackScore')));
		bidder.setPositiveFeedbackPercent(Double.valueOf(xBidder.getValue('PositiveFeedbackPercent')));
		bidder.setFeedbackPrivate(Boolean.valueOf(xBidder.getValue('FeedbackPrivate')));
		bidder.setFeedbackRatingStar(xBidder.getValue('FeedbackRatingStar'));
		bidder.setIDVerified(Boolean.valueOf(xBidder.getValue('IDVerified')));
		bidder.seteBayGoodStanding(Boolean.valueOf(xBidder.getValue('eBayGoodStanding')));
		bidder.setNewUser(Boolean.valueOf(xBidder.getValue('NewUser')));
		bidder.setRegistrationDate(Datetime.valueOf(xBidder.getValue('RegistrationDate')));
		bidder.setSite(xBidder.getValue('Site'));
		bidder.setStatus(xBidder.getValue('Status'));
		bidder.setUserID(xBidder.getValue('UserID'));
		
		BuyerType.BuyerType_e info = new BuyerType.BuyerType_e();
		bidder.setBuyerInfo(info);
		XMLDom.Element xInfo = xBidder.getElementByTagName('BuyerInfo');
		
		AddressType.AddressType_e shipping = new AddressType.AddressType_e();
		info.setShippingAddress(shipping);
		XMLDom.Element xShipping = xInfo.getElementByTagName('ShippingAddress');
		
		shipping.setCountry(xShipping.getValue('Country'));
		shipping.setPostalCode(xShipping.getValue('PostalCode'));
   	 	 
   		return resp;
   	}
}